
*-----------------------------------------------------------------------------*
* File: Power Calculation
*-----------------------------------------------------------------------------*

clear all


global n = 22
global N = 1600
global P = 0.5
global cluster = 108
global c1 = 0.7 //fraction of children that regularly consume the MDM
global c2 = 0.8 //Alternative value for c1
global c3 = 0.9 //Alternative value for c1
global s = 0.00 //few children in control schools that consume the MDM

use "Jehanabad_fulldataset_prep"
drop if wave==1

clear matrix
mat A1 = J(15,1,.)
mat A2 = J(15,1,.)
mat A3 = J(15,1,.)
mat A4 = J(15,1,.)
mat A5 = J(15,1,.)

*For some reason the hemoglobin variable (or its labeling) connot be used in loneway.
*When creating a new variable it workes though
gen hemo=hemoglobin

local r 1


foreach var in hemo any_an mild_an mod_an sev_an anemia_symptoms perceived_childhealth block_design_norm digit_span_forward_norm digit_span_backward_norm progressive_matrices_norm day_and_night_norm reading_norm math_norm total_attendance_2{
sum `var'
local sd =`r(sd)'
display `sd'
mat A1[`r',1] = `sd'
loneway `var' school_id
local rho_base = `r(rho)'  //ICC measured at baseline
display `rho_base'
mat A2[`r',1] = `rho_base'

local MDE_c1  (1.96 + 0.84)* `sd' *sqrt(1/( $P *(1- $P )* $cluster ))*sqrt( `rho_base' +((1- `rho_base')/ $n ))*(1/( $c1 - $s ))
display `MDE_c1'
mat A3[`r',1] = `MDE_c1'
local MDE_c2 (1.96 + 0.84)* `sd' *sqrt(1/( $P *(1- $P )* $cluster ))*sqrt(`rho_base'+((1-`rho_base' )/ $n ))*(1/( $c2 - $s ))
display `MDE_c2'
mat A4[`r',1] = `MDE_c2'
local MDE_c3 (1.96 + 0.84)* `sd' *sqrt(1/( $P *(1- $P )* $cluster ))*sqrt( `rho_base' +((1- `rho_base' )/ $n ))*(1/( $c3 - $s ))
display `MDE_c3'
mat A5[`r',1] = `MDE_c3'

local MDE_c1
local MDE_c2
local MDE_c3
local rho_base
local sd

local r `r'+1
}  

matrix rownames A1 = "Hemoglobin" "Any anemia" "Mild anemia" "Moderate anemia" "Severe anemia" "No anemia symptoms" "Perceived child health" "Block design" "Digit span forwards" "Digit span backwards" "Progressive Matrices" "Day and night" "Math" "Reading" "Attendance"

frmttable , statmat(A1) replace sdec(5) title (Table A2: MDE for differnt outcomes and different % of take-up) ctitles("", "sd") note("SD: Standard deviation. ICC: Intercluster corellation. MDE: Mininal detectable effect. C: Take up rate. Assumptions: Sample size: 1600, number of clusters: 108 schools, deviation of observations between treatment and control: 50:50. Hemoglobin in expressed in g/dl and the different forms of anemia represent percentage points). Cognition and education outcomes are normalized with respect to the control group mean and standard deviation..")
frmttable , statmat(A2) replace sdec(5) merge ctitles("Baseline ICC") 
frmttable , statmat(A3) replace sdec(5) merge ctitles("MDE (c = 0.7)") 
frmttable , statmat(A4) replace sdec(5) merge ctitles("MDE (c = 0.8)")  
frmttable , statmat(A5) replace sdec(5) merge ctitles("MDE (c = 0.9)") 
frmttable using "table_power_paper.doc", replace

